package com.dd.ent.car.common.player;

/**
 * Created by wx on 2016/3/3.
 */
public class ThunderMediaPlayerModel {

    private MessageReceiver mMessageReceiver;

    public final static int PLAYER_STATE_IDLE = 0;
    public final static int PLAYER_STATE_INITIALIZED = 1;
    public final static int PLAYER_STATE_PREPARING = 2;
    public final static int PLAYER_STATE_PREPARED = 3;
    public final static int PLAYER_STATE_STARTED = 4;
    public final static int PLAYER_STATE_LOADING = 5;
    public final static int PLAYER_STATE_PAUSED = 6;
    public final static int PLAYER_STATE_STOPPED = 7;
    public final static int PLAYER_STATE_COMPLETE = 8;
    public final static int PLAYER_STATE_ERROR = 9;


    private int mPlayerState = PLAYER_STATE_IDLE;
    private int mPlayerDuration = 0;
    private int mCurrentPosition = 0;
    private int mBufferedPosition = 0;

    private String mTitle = null;

    private int mMovieLength;

    public ThunderMediaPlayerModel(MessageReceiver messageReceiver) {
        mMessageReceiver = messageReceiver;
    }

    public int getPlayerState() {
        return mPlayerState;
    }

    public void setPlayerState(int playerState) {
        if (mPlayerState != playerState) {
            mPlayerState = playerState;
            Cargo cargo = Cargo.obtain();
            cargo.put(CargoId.PLAYER_STATE, mPlayerState);
            mMessageReceiver.receiveMessage(MessageId.PLAYER_MODEL_CHANGE_PLAYER_STATE, cargo);
            cargo.recycle();
        }

    }

    public int getPlayerDuration() {
        return mPlayerDuration;
    }

    public int getMovieLength() {
        return mMovieLength;
    }

    public void setMovieLength(int movieLength) {
        mMovieLength = movieLength;
    }

    public void setPlayerDuration(int playerDuration) {
        if (mPlayerDuration != playerDuration) {
            mPlayerDuration = playerDuration;
            Cargo cargo = Cargo.obtain();
            cargo.put(CargoId.PLAYER_DURATION, mPlayerDuration);
            mMessageReceiver.receiveMessage(MessageId.PLAYER_MODEL_CHANGE_PLAYER_DURATION, cargo);
            cargo.recycle();
        }
    }

    public int getCurrentPosition() {
        return mCurrentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        if (mCurrentPosition != currentPosition) {
            mCurrentPosition = currentPosition;
            Cargo cargo = Cargo.obtain();
            cargo.put(CargoId.PLAYER_CURRENT_POSITION, mCurrentPosition);
            mMessageReceiver.receiveMessage(MessageId.PLAYER_MODEL_CHANGE_CURRENT_POSITION, cargo);
            cargo.recycle();
        }
    }

    public int getBufferedPosition() {
        return mBufferedPosition;
    }

    public void setBufferedPercent(int bufferedPercent) {
        int bufferedPosition = mPlayerDuration * bufferedPercent / 100;
        if (mBufferedPosition != bufferedPosition) {
            mBufferedPosition = bufferedPosition;
            Cargo cargo = Cargo.obtain();
            cargo.put(CargoId.PLAYER_BUFFERED_POSITION, mBufferedPosition);
            mMessageReceiver.receiveMessage(MessageId.PLAYER_MODEL_CHANGE_BUFFERED_POSITION, cargo);
            cargo.recycle();
        }
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        if (title == null && mTitle == null) return;
        if (title == null || !title.equals(mTitle)) {
            mTitle = title;
            Cargo cargo = Cargo.obtain();
            cargo.put(CargoId.PLAYER_TITLE, mTitle);
            mMessageReceiver.receiveMessage(MessageId.PLAYER_MODEL_CHANGE_TITLE, cargo);
            cargo.recycle();
        }
    }
}
